package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.exception.ApiException;
import com.ksd.modules.course.entity.AdminCourseUserBuy;
import com.ksd.modules.course.mapper.AdminCourseUserBuyMapper;
import com.ksd.modules.course.po.AdminCourseUserBuySavePO;
import com.ksd.modules.course.po.AdminCourseUserBuySearchPO;
import com.ksd.modules.course.service.IAdminCourseService;
import com.ksd.modules.course.service.IAdminCourseUserBuyService;
import com.ksd.modules.course.vo.AdminCourseUserBuyVo;
import com.ksd.modules.course.vo.AdminCourseVo;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminCourseUserBuyServiceImpl extends BaseServiceImpl<AdminCourseUserBuyMapper, AdminCourseUserBuy> implements IAdminCourseUserBuyService {


    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminCourseService adminCourseService;

    /**
     * 查询列表
     *
     * @param adminCourseUserBuySearchPO
     * @return
     */
    public Page<AdminCourseUserBuyVo> findAdminCourseUserBuys(AdminCourseUserBuySearchPO adminCourseUserBuySearchPO) {
        Page<AdminCourseUserBuy> page = new Page<>(adminCourseUserBuySearchPO.getPageNo(), adminCourseUserBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminCourseUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseUserBuy::getSystemId, SystemIdThrealLocal.get().getSystemId());
        // 订单编号
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseUserBuySearchPO.getOrderno()), AdminCourseUserBuy::getOrderno, adminCourseUserBuySearchPO.getOrderno());
        // 支付方式
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseUserBuySearchPO.getPayMethod()), AdminCourseUserBuy::getPayMethod, adminCourseUserBuySearchPO.getPayMethod());
        // 具体资源订单
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseUserBuySearchPO.getOpid()), AdminCourseUserBuy::getOpid, adminCourseUserBuySearchPO.getOpid());
        // 兑换码
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseUserBuySearchPO.getCode()), AdminCourseUserBuy::getCode, adminCourseUserBuySearchPO.getCode());
        // uuid
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseUserBuySearchPO.getUuid()), AdminCourseUserBuy::getUuid, adminCourseUserBuySearchPO.getUuid());
        // 手机
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseUserBuySearchPO.getPhone()), AdminCourseUserBuy::getPhone, adminCourseUserBuySearchPO.getPhone());
        // 模糊匹配
        if (StringUtils.isNotEmpty(adminCourseUserBuySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(
                            AdminCourseUserBuy::getNickname, adminCourseUserBuySearchPO.getKeyword())
                    .or().like(AdminCourseUserBuy::getUsername, adminCourseUserBuySearchPO.getKeyword())
                    .or().like(AdminCourseUserBuy::getTitle, adminCourseUserBuySearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminCourseUserBuy::getCreateTime);
        Page<AdminCourseUserBuy> adminCoursePage = this.page(page, lambdaQueryWrapper);
        Page<AdminCourseUserBuyVo> adminCourseVoPage = tranferPageBo(adminCoursePage, AdminCourseUserBuyVo.class);
        return adminCourseVoPage;
    }

    /**
     * 保存和更新
     *
     * @param adminCourseUserBuySavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AdminCourseUserBuyVo saveUpdateAdminCourseUserBuy(AdminCourseUserBuySavePO adminCourseUserBuySavePO) {
        AdminCourseUserBuy adminCourse = tranferBo(adminCourseUserBuySavePO, AdminCourseUserBuy.class);
        adminCourse.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminCourse.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminCourse);
        AdminCourseUserBuyVo adminCourseVo = tranferBo(adminCourse, AdminCourseUserBuyVo.class);
        return adminCourseVo;
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminCourseUserBuyVo getAdminCourseUserBuyById(Long id) {
        AdminCourseUserBuy adminCourse = this.getById(id);
        AdminCourseUserBuyVo adminCourseVo = tranferBo(adminCourse, AdminCourseUserBuyVo.class);
        return adminCourseVo;
    }

    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminCourseUserBuyById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     *
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemovesAdminCourseUserBuy(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }

    /**
     * 用户绑定课程订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid) {
        LambdaQueryWrapper<AdminCourseUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseUserBuy::getUuid, uuid);
        lambdaQueryWrapper.eq(AdminCourseUserBuy::getOpid, opid);
        AdminCourseUserBuy adminCourseUserBuy = this.getOne(lambdaQueryWrapper);
        if (adminCourseUserBuy != null) {
            throw new ApiException(501, "该用户已绑定此资源!!!");
        }
        adminCourseUserBuy = new AdminCourseUserBuy();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        AdminCourseVo adminCourseVo = adminCourseService.getAdminCourseById(opid);
        adminCourseUserBuy.setOpid(opid);
        adminCourseUserBuy.setUserId(adminUserVo.getId());
        adminCourseUserBuy.setNickname(adminUserVo.getNickName());
        adminCourseUserBuy.setUsername(adminUserVo.getUserName());
        adminCourseUserBuy.setAvatar(adminUserVo.getAvatar());
        adminCourseUserBuy.setUuid(adminUserVo.getUuid());
        adminCourseUserBuy.setPhone(adminUserVo.getTelephone());
        adminCourseUserBuy.setAddress(adminUserVo.getAddress());

        adminCourseUserBuy.setTitle(adminCourseVo.getTitle());
        adminCourseUserBuy.setDescription(adminCourseVo.getDescription());
        adminCourseUserBuy.setCover(adminCourseVo.getImg());
        adminCourseUserBuy.setCode("");
        adminCourseUserBuy.setPrice(Objects.nonNull(adminCourseVo.getPrice()) ? adminCourseVo.getPrice() : String.valueOf(adminCourseVo.getCoin()));

        adminCourseUserBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminCourseUserBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminCourseUserBuy.setPayMethod(4);
        adminCourseUserBuy.setPayMethodName("人工");

        adminCourseUserBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminCourseUserBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminCourseUserBuy.setOrderJson("");
        return this.saveOrUpdate(adminCourseUserBuy);
    }
}
